package com.applink.sell_proof.builder;

import java.util.Date;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import android.content.Context;
import android.text.Html;
import android.text.Spanned;
import android.text.format.DateFormat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.applink.sell_proof.R;
import com.applink.sell_proof.model.Tier;

public class TierBuilder extends AbstractListBuilder {

	
	public TierBuilder(Context context, ViewGroup parent) {
		super(context, parent);
	}

	@Override
	public void notifyDataChange() {
		mAdapter.notifyDataSetChanged();
	}

	@Override
	public void buildData() {

	}

	@Override
	public View buildConvertView() {
		View layout = mLayoutInflater.inflate(R.layout.tier_item, null);
		return layout;
	}

	@Override
	public ConcurrentHashMap<Integer, View> buildConvertViewContent(View base) {

		ConcurrentHashMap<Integer, View> views = new ConcurrentHashMap<Integer, View>();

		views.put(R.id.tier_description, base.findViewById(R.id.tier_description));
		views.put(R.id.tier_product, base.findViewById(R.id.tier_product));
		views.put(R.id.tier_quantity, base.findViewById(R.id.tier_quantity));
		views.put(R.id.tier_point, base.findViewById(R.id.tier_point));
		views.put(R.id.tier_qualify, base.findViewById(R.id.tier_qualify));
		views.put(R.id.redeem, base.findViewById(R.id.redeem));
		return views;
	}

	@Override
	public void builConvertViewContentData(int position, ConcurrentHashMap<Integer, View> views, List<? extends Object> data) {

		Tier tier = (Tier) mData.get(position);
		((TextView) views.get(R.id.tier_description)).setText(tier.getDescription());
		((TextView) views.get(R.id.tier_product)).setText(tier.getProduct());
		((TextView) views.get(R.id.tier_point)).setText(Integer.toString(tier.getAchivePoint()));
		((TextView) views.get(R.id.tier_quantity)).setText(String.format("%s - %s", tier.getMinEligible(), tier.getMaxEligible()));
		String starDate = DateFormat.getDateFormat(mContext).format(new Date(tier.getStartDate()));
		String endDate = DateFormat.getDateFormat(mContext).format(new Date(tier.getEndDate()));
		((TextView) views.get(R.id.tier_qualify)).setText(String.format("%s - %s", starDate, endDate));
		views.get(R.id.redeem).setVisibility(tier.isRedeemable() ? View.VISIBLE : View.GONE);

		Spanned description = Html.fromHtml(String.format("<b>Tier Description </b>%s", tier.getDescription()));
		((TextView) views.get(R.id.tier_description)).setText(description);
	}

}
